package com.whw.springboot.walk.controller;


import com.whw.springboot.walk.UseToken;
import com.whw.springboot.walk.config.GlobalConfig;
import com.whw.springboot.walk.entity.Location;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.service.LocationService;
import com.whw.springboot.walk.utils.ApplyUtil;
import com.whw.springboot.walk.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "位置模块")
@RestController
@RequestMapping("/api/location")
public class LocationController {

    @Resource
    private LocationService locationService;
    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "添加位置")
    @UseToken(level = 1)
    @PostMapping(value = "/add")
    public Message insertLocation(Location location){
        String locationId = "L"+ ApplyUtil.genApplyId();
        location.setLocationId(locationId);
        return locationService.addLocation(location);
    }

    @ApiOperation(value="上传图片")
    @PostMapping(value="/Images" , headers = "content-type=multipart/form-data")
    @UseToken(level = 0)
    public String getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        return globalConfig.hostUrl+"resource/"+ LoadUtil.upload(globalConfig.filePath,files, request);

    }

    @ApiOperation(value="删除位置")
    @DeleteMapping(value="/delLocation")
    @UseToken(level = 1)
    public Message delLocation(String locationId){
        return locationService.delLocation(locationId);
    }

    @ApiOperation(value="更新位置")
    @PutMapping(value = "/updateLocation")
    @UseToken(level = 1)
    public Message updateLocation(Location location){
        return locationService.update(location);
    }

    @ApiOperation(value="根据名字查位置")
    @GetMapping(value="/findByName")
    @UseToken(level = 0)
    public Message<Location> findLocationBuName(String locationName){
        return locationService.findLocationByName(locationName);
    }

    @ApiOperation(value="查所有的位置")
    @GetMapping(value="/findAll")
    @UseToken(level = 0)
    public Message<List<Location>> findAllLocation(){
        return locationService.findAllLocation();
    }

}
